package com.alkaid.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alkaid.commons.core.CacheKeyDefinition;
import com.alkaid.commons.entity.Lev;
import com.alkaid.user.mapper.LevMapper;
import com.alkaid.user.service.LevService;
import com.alkaid.user.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Kylin
 */
@Service
public class LevServiceImpl implements LevService {

    private LevMapper levMapper;

    private RedisUtils redisUtils;

    @Autowired
    public void setLevMapper(LevMapper levMapper) {
        this.levMapper = levMapper;
    }

    @Autowired
    public void setRedisUtils(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    @Override
    public List<Lev> getLevList() {
        Object levList = redisUtils.getFromRedis(CacheKeyDefinition.LEV_LIST_KEY);
        if (levList == null) {
            List<Lev> levS = levMapper.findAllLev();
            redisUtils.save2Redis(CacheKeyDefinition.LEV_LIST_KEY, levS, CacheKeyDefinition.CACHE_TIME_DEFAULT);
            return levS;
        }
        return JSON.parseArray(levList.toString(), Lev.class);
    }
}
